/*
(C) Copyright MarketLive. 2006. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning: This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/
package com.marketlive.entity;

import org.marketlive.entity.IAttributePage;

import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.io.Serializable;

/**
 * A map of a localizable attribute page keyed on locale.
 */
public class LocaleMap implements Serializable {

    /** 
     * Persistent Map of attribute pages keyed on locale.  Used to maintain Hibernate objects. 
     */
    private Map<String, IAttributePage> persistentLocaleAttributePages = new HashMap<String, IAttributePage>();
    /**
     * Transient Map of attribute pages keyed on locale.  Used to hold temporary objects. 
     */
    private Map<String, IAttributePage> transientLocaleAttributePages = new HashMap<String, IAttributePage>();

    /**
     * Dirty flag to control iteration over localized pages.
     */
    private boolean pagesAreDirty = false;
    
    /**
     * Returns the collection of attribute pages for this locale map.  
     * May return null if attribute was set to null during Hibernate load.
     * @return the collection of attribute pages
     */
    @SuppressWarnings("unused")
    public Map<String, IAttributePage> getLocaleAttributePages() {
    	if (pagesAreDirty) {
    		for (Iterator itr = this.persistentLocaleAttributePages.keySet().iterator(); itr.hasNext();) {
    			IAttributePage page = this.persistentLocaleAttributePages.get(itr.next());
    			if (page.isEmpty()) {
    				itr.remove();
    			}
    		}
    	}
        for (String localeStr : this.transientLocaleAttributePages.keySet()) {
            IAttributePage page = this.transientLocaleAttributePages.get(localeStr);
            if (!page.isEmpty()) {
                if (this.persistentLocaleAttributePages == null) {
                    this.persistentLocaleAttributePages = new HashMap<String, IAttributePage>();
                }

                this.persistentLocaleAttributePages.put(localeStr, page);
            }
        }
        return this.persistentLocaleAttributePages;
    }

    /**
     * Sets the collection of attribute pages. Hibernate use only.
     * @param localeAttributePages
     */
    @SuppressWarnings("unused")
    private void setLocaleAttributePages(Map<String, IAttributePage> localeAttributePages) {
        this.persistentLocaleAttributePages = localeAttributePages;
    }

    /**
     * Returns the attribute page for the specified locale.
     * @param locale
     * @return attribute page for specified locale or null if not found
     */
    public IAttributePage getPageByLocale(Locale locale) {

        IAttributePage page;
        if (this.persistentLocaleAttributePages == null
                || (page = this.persistentLocaleAttributePages.get(locale.toString())) == null) {
            return this.transientLocaleAttributePages.get(locale.toString());
        } else {
        	pagesAreDirty = true;
            return page;
        }
    }

    /**
     * Sets the attribute page in this locale map for the specified locale.
     * @param page attribute page
     * @param locale
     */
    public void setPageForLocale(IAttributePage page,Locale locale) {
        this.transientLocaleAttributePages.put(locale.toString(),page);
    }
}